package com.kyb.cms.config;

import com.kyb.cms.util.oss.AliYunOssUtil;
import com.kyb.cms.util.oss.ResourceApiConfig;
import lombok.extern.slf4j.Slf4j;
import org.armada.galileo.common.loader.SpringMvcUtil;
import org.armada.galileo.common.util.SpringConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiaobo
 * @date 2023/11/6 14:37
 */
@Configuration
@Slf4j
public class AliyunOssConfig {

    private ResourceApiConfig getResourceApiConfig(String prefix) {
        String publicEndpoint = SpringConfig.getConfig(prefix + ".publicEndpoint");
        String endpoint = SpringConfig.getConfig(prefix + ".endpoint");
        String accessKeyId = SpringConfig.getConfig(prefix + ".accessKeyId");
        String accessSecret = SpringConfig.getConfig(prefix + ".accessSecret");
        String bucketName = SpringConfig.getConfig(prefix + ".bucketName");

        ResourceApiConfig cfg = new ResourceApiConfig();

        cfg.setPublicEndpoint(publicEndpoint);
        cfg.setEndpoint(endpoint);
        cfg.setAccessKeyId(accessKeyId);
        cfg.setAccessSecret(accessSecret);
        cfg.setBucketName(bucketName);

        return cfg;
    }

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public AliYunOssUtil initAliYunOssUtil(){
        SpringMvcUtil.setContext(applicationContext);
        AliYunOssUtil ossUtil = AliYunOssUtil.instance(getResourceApiConfig("aliyun.oss.public"));
        return ossUtil;
    }

}
